﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;

namespace COFE.Core
{
    /// <summary>
    /// Indicate the property cachable in EntryPropertyValue Table.
    /// </summary>
    public class CachableAttribute : Attribute
    {
        public CachableAttribute()
        {
            DefaultValue = null;
        }

        public CachableAttribute(object defaultValue)
        {
            DefaultValue = defaultValue;
        }

        public object DefaultValue { get; private set; }
    }

     /// <summary>
    /// Extends MetadataAttribute.
    /// </summary>
    public static class CachableExtension
    {
        /// <summary>
        /// Whether a property is tagged with CachableAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool IsCachable<T>(T item)
        {
            return EnumAttributeUtils<CachableAttribute, T>.FindAttribute(item) != null;
        }

        /// <summary>
        /// Whether a property is tagged with NotCachableAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool IsNotCachable<T>(T item)
        {
            return EnumAttributeUtils<NotCachableAttribute, T>.FindAttribute(item) != null;
        }


        /// <summary>
        /// Get MetadataAttribute that tagged with the specified property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static object getDefaultValue<T>(T item)
        {
            return EnumAttributeUtils<CachableAttribute, T>.FindAttribute(item).DefaultValue;
        }
    }
}
